using System;
using System.Collections.Generic;
using System.Text;

namespace Multiplicity.Reflection{
    abstract class SizePrefixedHeapBase<T> : StorageHeap<T> {

        protected SizePrefixedHeapBase(Module module, StorageHeapHeader head, long storageOffset)
            : base(module, head, storageOffset) {
        }

        protected sealed override bool IsValidIndex(uint index) {
            return index < Header.Size;
        }

        protected sealed override T ReadItem(uint index) {

            uint idx = (uint)(BufferOffset + index);
            uint size = CompressedInt.Read(new BufferSegment(Buffer, idx));

            if (size == 0) {
                return default(T);
            }

            idx += CompressedInt.GetByteCount(size);

            return ReadBlobItem(idx, size);
        }

        protected abstract T ReadBlobItem(uint index, uint size);
    }
}
